package array;

/**
 * @author 十八岁讨厌编程
 * @date 2025/6/27 21:02
 * @PROJECT_NAME leetcode
 * @description
 */

public class Solution59 {
    public int[][] generateMatrix(int n) {
        int[][] result = new int[n][n];
        int x = 0;
        int y = 0;
        int direct_x = 0;
        int direct_y = 1;
        for (int i = 1; i <= n * n; i++) {
            result[x][y] = i;
            int try_x = x + direct_x;
            int try_y = y + direct_y ;
            if (try_x > n - 1 || try_y > n - 1 || try_x < 0 || try_y < 0 || result[try_x][try_y] != 0) {
                int temp = direct_y;
                direct_y = -direct_x;
                direct_x = temp;
            }
            x += direct_x;
            y += direct_y;
        }
        return result;
    }
}

